package fun.werfamily.wf.user.core.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.werfamily.wf.user.core.domain.constant.GeoConstant;
import fun.werfamily.wf.user.core.domain.constant.RedisConstant;
import fun.werfamily.wf.user.core.domain.constant.RedisTimeoutConstants;
import fun.werfamily.wf.user.core.domain.dto.req.GeoAddressReqDTO;
import fun.werfamily.wf.user.core.domain.dto.resp.GeoAddressDTO;
import fun.werfamily.wf.user.core.domain.service.IGeoAddressService;
import fun.werfamily.wf.user.core.domain.util.RedisUtil;
import fun.werfamily.wf.user.core.infrastructure.dataobject.GeoDictDO;
import fun.werfamily.wf.user.core.infrastructure.mapper.GeoDictMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class IGeoAddressServiceImpl extends ServiceImpl<GeoDictMapper, GeoDictDO> implements IGeoAddressService {

    @Resource
    private RedisUtil redisUtil;

    /**
     * 根据地址编码获取下一级所有地址信息
     *
     * @return
     */
    @Override
    public List<GeoAddressDTO> listAllChildByGeoCode(GeoAddressReqDTO geoAddressReqDTO) {
        // TODO 缓存
        String geoCode = geoAddressReqDTO.getGeoCode();
        if (StringUtils.isBlank(geoCode)) {
            geoCode = GeoConstant.ROOT_GEO_CODE;
        }
        // 获取缓存
        List<GeoAddressDTO> childList = redisUtil.getListByRange(RedisConstant.CHILD_GEO_ADDRESS_LIST + geoCode, 0L, -1L, GeoAddressDTO.class);
        if (CollectionUtils.isNotEmpty(childList)) {
            return childList;
        }

        childList = new ArrayList<>();
        // 查询数据库
        QueryWrapper<GeoDictDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GeoDictDO::getParentCode, geoCode);
        queryWrapper.lambda().eq(GeoDictDO::getStatus, GeoConstant.GEO_NORMAL_STATUS);
        List<GeoDictDO> childGeoList = list(queryWrapper);
        if (CollectionUtils.isNotEmpty(childGeoList)) {
            for (GeoDictDO geoDictDO : childGeoList) {
                GeoAddressDTO geoAddressDTO = new GeoAddressDTO();
                BeanUtils.copyProperties(geoDictDO, geoAddressDTO);
                childList.add(geoAddressDTO);
            }
            childList = childList.stream().sorted(Comparator.comparing(geoAddressDTO -> Long.valueOf(geoAddressDTO.getGeoCode()))).collect(Collectors.toList());
            redisUtil.listRightPushAll(RedisConstant.CHILD_GEO_ADDRESS_LIST + geoCode, childList);
            redisUtil.setExpire(RedisConstant.CHILD_GEO_ADDRESS_LIST + geoCode, RedisTimeoutConstants.CACHE_HOLD_TIME_1DAY);
        }
        return childList;
    }
}
